// pages/integrallotteryrecord/integrallotteryrecord.js
const util = require('../../utils/util.js');
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winRecord: [],
    totalCount: false,
    loading: false,
    arrlength: 0,
    index: 1,
    page: 20,
    dataArr: [],
    /**提示前往填写收货地址 */
    isCertification: false,
    /**填写收货地址弹框 */
    msg: false,
    recordid: "",
    top: "",
    ShowAgreement: false,
    /**同意协议0 */
    isAgreed: false,
    recordType: 0,
    /** */
    cdkData: "",
    isCopied1: null,
    isClickCopy: false,
    windowHeight: 1106,
    //input数据
    input: {
      mobile: null,
      realName: "",
      IDcard: "",
      province: "",
      city: "",
      area: "",
      detailedAddress: "",

    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.windowHeight) {
      var height = app.globalData.windowHeight;
      this.data.windowHeight = height;
      this.setData({
        windowHeight: this.data.windowHeight
      })
    }
    if (GlobalData.prixzeAddress) {
      this.data.msg = true;
      this.data.input.realName = GlobalData.prixzeAddress.name;
      this.data.input.mobile = GlobalData.prixzeAddress.phone;
      this.data.input.city = GlobalData.prixzeAddress.areas + GlobalData.prixzeAddress.address;
    }
    this.setData({
      msg: this.data.msg,
      input: this.data.input
    })

    dataManager.rigisterObserver("getdrawrecordlist", this.onComplete, this);
    dataManager.getWinRecordDm(this.data.recordType, this.data.index, this.data.page, false);
  },
  onComplete() {
    console.log(GlobalData.inteWinRecord[this.data.recordType])
    this.data.dataArr = GlobalData.inteWinRecord[this.data.recordType]
    if (GlobalData.inteWinRecord[this.data.recordType].length == this.data.arrlength)
      return;
    // if (GlobalData.inteWinRecord[this.recordType].length) {
    // this.data = GlobalData.inteWinRecord[this.recordType]
    this.data.loading = false;
    this.data.arrlength = this.data.dataArr.length;

    if (this.data.dataArr.length) {
      this.data.totalCount = true;
    } else {
      this.data.totalCount = false;
    }
    this.setData({
      dataArr: this.data.dataArr,
      totalCount: this.data.totalCount
    })
    console.log(this.data.dataArr)
    // }
  },
  onReceive(index) {
    if ((index["type"] == "2") && (index["state"] == 0)) {
      this.data.isCertification = true;
      this.data.recordid = index["receiveid"];
      GlobalData.recordid = index["receiveid"];
      this.setData({
        isCertification: this.data.isCertification
      })
    }
  },

  onClose() {
    this.data.isCertification = false;
    this.data.msg = false;
    this.setData({
      isCertification: this.data.isCertification,
      msg: this.data.msg
    });
    GlobalData.prixzeAddress = null;
  },
  onCertification() {
    this.data.isCertification = false;
    this.setData({
      isCertification: this.data.isCertification,
    })
    dataManager.UseNavigateTo("/pages/addressmanage/addressmanage?url=integrallotteryrecord", false);

  },

  /**确认填写地址 */
  onInformation() {

    if (!this.data.isAgreed) {
      return;
    }
    if (this.data.input.realName == "" || this.data.input.realName == null) {
      app.onshowModal("请输入您的名字");
      return;
    }
    if (this.data.input.mobile == "" || this.data.input.mobile == null) {
      app.onshowModal("请输入正确的手机号码");
      return;
    }
    if (this.data.input.city == "" || this.data.input.city == null) {
      app.onshowModal("请输入收货的省份，城市，地区");
      return;
    }
    var address = this.data.input.city;
    try {
      let url = { method: "updatedrawrecordconsignee", Token: app.getToken(), "recordid": GlobalData.recordid, "consigneename": this.data.input.realName, "consigneeaddress": address, "consigneemobile": this.data.input.mobile };
      dataManager.OnRequestPost(url, this.updateDrawRecordConsigneeComplete);
    } catch (error) {
      console.log(error)
    }
  },
  updateDrawRecordConsigneeComplete() {
    this.data.msg = false;
    var that = this;
    wx.showModal({
      title: '提示',
      content: "添加地址成功。",
      showCancel: false,
      success: function (res) {
        that.setData({
          msg: that.data.msg
        })
        GlobalData.prixzeAddress = null;

        dataManager.rigisterObserver("getdrawrecordlist", this.onComplete, this);
        dataManager.getWinRecordDm(this.data.recordType, 1, this.data.page, false);
      }
    })


  },

  /**关闭协议提示 */
  onCloseAgreement() {
    this.data.ShowAgreement = false;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
    })
  },
  /**显示对勾或关闭 */
  onCloseGou() {
    this.data.isAgreed = !this.data.isAgreed;
    this.setData({
      isAgreed: this.data.isAgreed,
    })
  },

  /**查看协议信息 */
  onCheckText() {
    this.data.ShowAgreement = true;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
    })

  },
  /** 不同意*/
  onDisagreed() {
    this.data.ShowAgreement = false;
    this.data.isAgreed = false;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
      isAgreed: this.data.isAgreed
    })
  },
  /** 同意协议0 */
  onAgreed() {
    this.data.ShowAgreement = false;
    this.data.isAgreed = true;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
      isAgreed: this.data.isAgreed
    })
  },
  /**复制cdk 00*/
  onCopy(e) {
    let item = e.currentTarget.dataset.item
    console.log(e)
    GlobalData.recordid = item.recordid
    /**房卡类 复制cdk */
    if (item.type == 3) {
      this.data.cdkData = item.cdkey;
      this.isClickCopy = true;
      this.setData({
        cdkData: this.data.cdkData,
      })
      wx.setClipboardData({
        data: this.data.cdkData,
        success() {
          wx.getClipboardData({
            success(res) {
              app.onshowModal("已复制成功。")
            }
          })
        },
        fail(res) {
          console.error(res)
        }
      })

      return;
    }
    if (item.type == 2) {
      if (item.isaddconsignee == 1) {
        /**提醒发货 */
        try {
          let url = { method: "drawrecordremind", Token: app.getToken(), "recordid": GlobalData.recordid };
          dataManager.OnRequestPost(url, this.DrawDecordRemindComplete);
        } catch (error) {
          console.log(error)
        }
      } else {
        /**前往收货地址选择地址 */
        this.data.isCertification = true;
        this.setData({
          isCertification: this.data.isCertification,
        })
      }

    }
  },
  DrawDecordRemindComplete() {
    this.data.msg = false;
    this.setData({
      msg: this.data.msg,
    })
    app.onshowModal("已提醒发货。");
    GlobalData.isCertification = true;
  },
  onInfoClose() {
    this.data.isClickCopy = false;
    this.setData({
      isClickCopy: this.data.isClickCopy,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log("页面上拉触底事件的处理函数")
    // if (!this.data.loading) {
    //   this.data.loading = true;
    //   // this.app.loadShow();
    //   this.data.index += 1;
    //   dataManager.rigisterObserver("getdrawrecordlist", this.onComplete, this);
    //   dataManager.getWinRecordDm(this.data.recordType, this.data.index, this.data.page, true);
    // }
  },
  lower: function (e) {
    console.log("页面上拉触底事件的处理函数")
    if (!this.data.loading) {
      this.data.loading = true;
      // this.app.loadShow();
      this.data.index += 1;
      dataManager.rigisterObserver("getdrawrecordlist", this.onComplete, this);
      dataManager.getWinRecordDm(this.data.recordType, this.data.index, this.data.page, true);
    }
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})